package cn.com.oceansoft.business.danger.controller;

import cn.com.oceansoft.business.base.controller.BaseController;
import cn.com.oceansoft.business.base.entity.Pager;
import cn.com.oceansoft.business.base.entity.Query;
import cn.com.oceansoft.business.base.entity.Result;
import cn.com.oceansoft.business.danger.entity.*;
import cn.com.oceansoft.business.danger.service.ICollecterInfoService;
import cn.com.oceansoft.business.danger.service.IPtcsbzService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.List;

/**
 * Created by OS on 2018/5/28.
 */

@Controller
@RequestMapping("danger/ptcsbz")
public class PtcsbzController extends BaseController {

    @Resource
    private IPtcsbzService ptcsbzService;
    @Resource
    private ICollecterInfoService collecterInfoService;

    @RequestMapping(value = "qlist")
    public String qlist(Model model, Query query, CollecterInfo collecterInfo,HttpServletRequest request) {
        CollecterInfo cInfo =(CollecterInfo)request.getSession().getAttribute("aqyhuser");
        if("".equals(collecterInfo.getFjid())||null==collecterInfo.getFjid()){
            collecterInfo.setFjid(getMemberSession().getFjid());
            collecterInfo.setPcsid("");
            collecterInfo.setSqid("");
        }
        if("0".equals(cInfo.getRoletype())){
            if(cInfo.getFjid()!=null){
                collecterInfo.setFjid(cInfo.getFjid());
            }
        }
        query.setData(collecterInfo);
        model.addAttribute("query",query);
        model.addAttribute("pager",ptcsbzService.getPtcsbzList(query));
        if("".equals(collecterInfo.getFjid())||null==collecterInfo.getFjid()){
            model.addAttribute("dicone",collecterInfoService.getdict(collecterInfo));
        }else{
            CollecterInfo one=new CollecterInfo();
            one.setFjid(collecterInfo.getFjid());
            if("".equals(getMemberSession().getFjid())||null==getMemberSession().getFjid()){
                CollecterInfo collecterInfo1=new CollecterInfo();
                collecterInfo1.setFjid("");
                model.addAttribute("dicone",collecterInfoService.getdict(collecterInfo1));
            }else{
                model.addAttribute("dicone",collecterInfoService.getonedict(one));
            }
            model.addAttribute("dictwo",collecterInfoService.getdict(one));
            if(!"".equals(collecterInfo.getPcsid())&&null!=collecterInfo.getPcsid()){
                one.setPcsid(collecterInfo.getPcsid());
                model.addAttribute("dicthree",collecterInfoService.getdict(collecterInfo));
            }
        }
        return "danger/ptcsbz/ptcsbz_qlist";
    }

    @RequestMapping(value = "jdlist")
    public String jdlist(Model model, Query query, CollecterInfo collecterInfo,HttpServletRequest request) {
        CollecterInfo cInfo =(CollecterInfo)request.getSession().getAttribute("aqyhuser");
        if("".equals(collecterInfo.getFjid())||null==collecterInfo.getFjid()){
            collecterInfo.setFjid(getMemberSession().getFjid());
            collecterInfo.setPcsid("");
            collecterInfo.setSqid("");
        }
        if("1".equals(cInfo.getRoletype())){
            collecterInfo.setPcsid(cInfo.getPcsid());
        }
        query.setData(collecterInfo);
        model.addAttribute("query",query);
        model.addAttribute("pager",ptcsbzService.getPtcsbzList(query));
        if("".equals(collecterInfo.getFjid())||null==collecterInfo.getFjid()){
            model.addAttribute("dicone",collecterInfoService.getdict(collecterInfo));
        }else{
            CollecterInfo one=new CollecterInfo();
            one.setFjid(collecterInfo.getFjid());
            if("".equals(getMemberSession().getFjid())||null==getMemberSession().getFjid()){
                CollecterInfo collecterInfo1=new CollecterInfo();
                collecterInfo1.setFjid("");
                model.addAttribute("dicone",collecterInfoService.getdict(collecterInfo1));
            }else{
                model.addAttribute("dicone",collecterInfoService.getonedict(one));
            }
            model.addAttribute("dictwo",collecterInfoService.getdict(one));
            if(!"".equals(collecterInfo.getPcsid())&&null!=collecterInfo.getPcsid()){
                one.setPcsid(collecterInfo.getPcsid());
                model.addAttribute("dicthree",collecterInfoService.getdict(collecterInfo));
            }
        }
        return "danger/ptcsbz/ptcsbz_jdlist";
    }

    @RequestMapping("thpage")
    public String thPage(HttpServletRequest request,Model model){
        String isconfirm = request.getParameter("isconfirm");
        String ptcsbzid = request.getParameter("ptcsbzid");
        model.addAttribute("isconfirm",isconfirm);
        model.addAttribute("ptcsbzid",ptcsbzid);
        return "danger/ptcsbz/ptcsbz_th";
    }

    @ResponseBody
    @RequestMapping("qrth")
    public Result qrth(HttpServletRequest request, PtcsbzFlow ptcsbzFlow){
        CollecterInfo cInfo =(CollecterInfo)request.getSession().getAttribute("aqyhuser");
        String isconfirm = request.getParameter("isconfirm");
        if(isconfirm!=null){
            if(isconfirm.equals("1")){
                ptcsbzFlow.setSpjg("1");
                ptcsbzFlow.setStep("1");
            }else if(isconfirm.equals("2")){
                ptcsbzFlow.setSpjg("0");
                ptcsbzFlow.setStep("1");
            }else if(isconfirm.equals("3")){
                ptcsbzFlow.setSpjg("0");
                ptcsbzFlow.setStep("2");
            }else if(isconfirm.equals("4")){
                ptcsbzFlow.setSpjg("1");
                ptcsbzFlow.setStep("2");
            }
        }
        ptcsbzFlow.setId(UUID.randomUUID().toString());
        ptcsbzFlow.setUserid(cInfo.getId());
        Result result = new Result();
        try {
            ptcsbzService.PtcsbzQrth(ptcsbzFlow, isconfirm);
            result.setMsg("操作成功!");
        }catch (Exception e){
            e.printStackTrace();
            result.setSucc(result.FAILURE);
            result.setMsg("操作失败!");

        }
        return result;
    }

    @RequestMapping("detail")
    public String detail(Model model, HttpServletRequest request) {
        String id= request.getParameter("id");
        Ptcsbz ptcsbz = ptcsbzService.findByid(id);
        //获取所有图片列表
        List<Attach> attach = ptcsbzService.findAllAttach(id);
        //获取流程信息
        List<PtcsbzFlow> flows = ptcsbzService.findAllFlowsbyPtcsbzd(id);
        model.addAttribute("data",ptcsbz);
        model.addAttribute("attach",attach);
        model.addAttribute("flows",flows);
        return "danger/ptcsbz/info_detail";
    }

    @RequestMapping("ptcsbztj/{deep}")
    public String ptcsbztj(@PathVariable("deep") int deep, Model model, HttpServletRequest request, Query query) {
        //根据层级和id来确定数据
        String id= request.getParameter("id");
        CollecterInfo cInfo =(CollecterInfo)request.getSession().getAttribute("aqyhuser");
        if(deep==1){
            if("0".equals(cInfo.getRoletype())){
                if(cInfo.getFjid()==null||("".equals(cInfo.getFjid()))){
                    deep = 1;
                }else{
                    deep = 2;
                    id=cInfo.getFjid();
                }
            }
            if("1".equals(cInfo.getRoletype())){
                deep = 3;
                id=cInfo.getPcsid();
            }
            if("2".equals(cInfo.getRoletype())){
                deep = 4;
                id=cInfo.getSqid();
            }
        }
        model.addAttribute("cInfo",cInfo);
        query.setData(id);
        model.addAttribute("timeChoose",query);
        List<Ptcsbz> list= new ArrayList<Ptcsbz>();
        list=ptcsbzService.ptcsbztj(deep,query);
        Ptcsbz total = new Ptcsbz();
        total.setName("合计");
        total.setId("-1");
        for(int i =0;i<list.size();i++){
            total.setMhq(total.getMhq()+ list.get(i).getMhq());
            total.setYjzmd(total.getYjzmd()+ list.get(i).getYjzmd());
            total.setJypl(total.getJypl()+ list.get(i).getJypl());
            total.setDlygbjq(total.getDlygbjq()+ list.get(i).getDlygbjq());
            total.setDqhzjkxt(total.getDqhzjkxt()+ list.get(i).getDqhzjkxt());
            total.setKrqtjjqdzz(total.getKrqtjjqdzz()+ list.get(i).getKrqtjjqdzz());
            total.setDdcgs(total.getDdcgs()+ list.get(i).getDdcgs());
            total.setDdcmj(total.getDdcmj()+ list.get(i).getDdcmj());
            total.setDdctfl(total.getDdctfl()+ list.get(i).getDdctfl());
        }
        list.add(total);
        model.addAttribute("list",list);
        if(deep==1)
        {
            return "danger/ptcsbz/fj_list";
        } else      if(deep==2)
        {
            return "danger/ptcsbz/pcs_list";
        }else{
            return "danger/ptcsbz/sq_list";
        }
    }

    @RequestMapping("ptcsbztj/detail/{deep}")
    public String xxtjDetail(@PathVariable("deep") int deep, Model model, HttpServletRequest request, CollecterInfo cInfo, Query query) {
        //根据层级和地区和类别来确定数据
        String fjid = request.getParameter("fjid");//区
        String pcsid = request.getParameter("pcsid");//街道
        String sqid = request.getParameter("sqid");//社区
        //排除合计
        fjid = Objects.equals(fjid, "-1") ? null : fjid;
        pcsid = Objects.equals(pcsid, "-1") ? null : pcsid;
        sqid = Objects.equals(sqid, "-1") ? null : sqid;
        cInfo.setFjid(fjid);
        cInfo.setPcsid(pcsid);
        cInfo.setSqid(sqid);
        String column = request.getParameter("column");

        Map<String, String> map = new HashMap<>();
        map.put("column", column);
        query.setMap(map);
        query.setData(cInfo);
        model.addAttribute("cInfo", cInfo);
        model.addAttribute("timeChoose", query);
        model.addAttribute("query", query);
        model.addAttribute("column", column);
        model.addAttribute("pager",ptcsbzService.getPtcsbzDetailList(query));
        if (deep == 1) {
            return "danger/ptcsbz/detail_fj_list";
        } else if (deep == 2) {
            return "danger/ptcsbz/detail_pcs_list";
        } else {
            return "danger/ptcsbz/detail_sq_list";
        }
    }

    @RequestMapping("ptcsbzExport/{deep}")
    public void ptcsbzExport(@PathVariable("deep") int deep,Model model, HttpServletRequest request,Query query,HttpServletResponse response) {
        //根据层级和id来确定数据
        String id= "";
        CollecterInfo cInfo =(CollecterInfo)request.getSession().getAttribute("aqyhuser");
        if(deep==1){
            if("0".equals(cInfo.getRoletype())){
                if(cInfo.getFjid()==null||("".equals(cInfo.getFjid()))){
                    deep = 1;
                }else{
                    deep = 2;
                    id=cInfo.getFjid();
                }
            }
            if("1".equals(cInfo.getRoletype())){
                deep = 3;
                id=cInfo.getPcsid();
            }
            if("2".equals(cInfo.getRoletype())){
                deep = 4;
                id=cInfo.getSqid();
            }
        }
        query.setData(id);
        List<Ptcsbz> list= new ArrayList<Ptcsbz>();
        list=ptcsbzService.ptcsbztj(deep,query);
        Ptcsbz total = new Ptcsbz();
        total.setName("合计");
        total.setId("-1");
        for(int i =0;i<list.size();i++){
            total.setMhq(total.getMhq()+ list.get(i).getMhq());
            total.setYjzmd(total.getYjzmd()+ list.get(i).getYjzmd());
            total.setJypl(total.getJypl()+ list.get(i).getJypl());
            total.setDlygbjq(total.getDlygbjq()+ list.get(i).getDlygbjq());
            total.setDqhzjkxt(total.getDqhzjkxt()+ list.get(i).getDqhzjkxt());
            total.setKrqtjjqdzz(total.getKrqtjjqdzz()+ list.get(i).getKrqtjjqdzz());
            total.setDdcgs(total.getDdcgs()+ list.get(i).getDdcgs());
            total.setDdcmj(total.getDdcmj()+ list.get(i).getDdcmj());
            total.setDdctfl(total.getDdctfl()+ list.get(i).getDdctfl());
        }
        list.add(total);
        ptcsbzService.exportPtcsbztj(response,list,deep);
    }
}
